// JavaScript Document
jQuery.fn.updateVote=function(url){
	var note=$("#voterNote").val();
	var boxes=$(".voteBox:checked");
	var choiceIds=new Array();
	var voterEmail = $("#voterEmail").val();
	var voterName = $("#voterName").val();
	if("Input you name" == voterName){
		voterName = " ";
	}
	
	for(var i=0;i<boxes.length;i++){
		var box=$(boxes[i]);
		choiceIds[i]=box.val();
	}
	var data={
		selectChoices:choiceIds.toLocaleString(),
		voterNote:note,
		voterEmail:voterEmail,
		voterName:voterName
	};
	var updateUrl="UpdateVoteServlet?url="+url;
	$.ajax({
		type:"post",
		async:false,
		url:updateUrl,
		data:data,
		success:function(result){
			if(result=="true"){
				$().showMessage("Save successes.");
			}else{
				$().showMessage("Save fails.");
			}
		},	
	});
};


jQuery.fn.fillTotal=function(){
	var totalArray=$('.hiddenTotal');
	for(var i=0;i<totalArray.length;i++){
		var l=totalArray[i];
		var label=$(l);
		var labelId=label.attr("id");
		var value=label.html();
		var choiceId=labelId.substr(11);
		var choiceId2="total"+choiceId;
		$("#"+choiceId2).html(value);
	}
};

jQuery.fn.highlightRecommend=function(){
	var labels=$(".recommendIdLabel");
	var checkBoxes=$(".voteBox");
	var voteDivs=$(".voteDiv");
	if(labels.length!=0){
		for(var i=0;i<labels.length;i++){
			for(var j=0;j<checkBoxes.length;j++){
				if($(labels[i]).html()==$(checkBoxes[j]).val()){
					$(voteDivs[j]).css("background-color","#147DB6");
					break;
				}
			}
		}
	}
};

jQuery.fn.getHistory = function(url){
	var voterEmail = $("#voterEmail").val();
	//var voterEmail = "loengf@hku.hk";

	var boxes=$(".voteBox");
	var choiceIds=new Array();
	for(var i=0;i<boxes.length;i++){
		var box=$(boxes[i]);
		choiceIds[i]=box.val();
	}
	var allChoices = choiceIds.toLocaleString();
	//$("#showLable").text(allChoices);
	//$("resultNote").val(allChoices);
	//alert(allChoices);
	
	
	if($().validEmail(voterEmail)){
		$("#emailLable").text(" Voter Email: "+voterEmail);
		$("#emailLable").show();
		$("#modifyEmailButton").show();
		$("#voterEmail").hide();
		$("#voterName").show();
		$("#saveButton").show();	
		var boxes=$(".voteBox");
		var choiceIds=new Array();
		for(var i=0;i<boxes.length;i++){
			var box=$(boxes[i]);
			choiceIds[i]=box.val();
		}
		var historyUrl="GetHistoryServlet?url="+url+"&voterEmail="+voterEmail+"&allChoices="+allChoices;
		//var historyUrl="GetHistoryServlet?url="+url+"&voterEmail="+voterEmail;
		var testUrl = "closeEvent?url="+url;
		var postData ={
			allChoices:choiceIds.toLocaleString(),
			voterEmail:voterEmail,
			eventLink:url
		};
		$.ajax({
			type:"get",
			async:false,
			url:historyUrl,
			success:function(data){
				if("empty" == data){
					//$().showMessage("empty");
					//$().clearAll();
				}else{
					//$().showMessage(data);
					$().setHistory(data);
					$("#clearAllButton").show();
					$().showMessage("Show History Vote!");
				}
			}
		});
		$("#clearAllButton").show();
	}else{
		alert("Invalid Emil, please input again!");
	}	
};

jQuery.fn.setHistory = function(data){
	if(data != null)
		{
			var boxes = $(".voteBox");
			for(var i=0; i<boxes.length; i++){
				if('t' == data[i]){
					$(boxes[i]).attr("checked", true);
				}else if('f' == data[i]){
					$(boxes[i]).attr("checked", false);
				}
			};
			
		};
};

jQuery.fn.validEmail = function(email){
	var patten = new RegExp(/^[\w-]+(\.[\w-]+)*@([\w-]+\.)+[a-zA-Z]+$/);
	return patten.test(email);
};

jQuery.fn.showMessage=function(msg){
	$("#messageDiv").html(msg);
	$("#messageDiv").fadeIn(1000);
	$("#messageDiv").fadeOut(3000);
};

jQuery.fn.clearAll = function(url){	
	$("[name='checkboxVote']").attr("checked",false);
	$("#clearAllButton").hide();
	$("#showHistoryButton").show();
	$().showMessage("Clear History Vote!");
};

jQuery.fn.inputEmail = function(){
	$("#voterEmail").val("");
};

jQuery.fn.inputName = function(){
	$("#voterName").val("");
};

jQuery.fn.inputNote = function(){
	$("#voterNote").val("");
};
jQuery.fn.test=function(){
	alert("test");
};

jQuery.fn.modify=function(){
	$("#voterEmail").show();
	$("#voterName").hide();
};

jQuery.fn.setVoterName = function(){
	$("#nameLable").text("Voter Name: "+$("#voterName").val());
	$("#nameLable").show();
};
jQuery.fn.showHistory=function(url){
	$("#showHistoryButton").hide();
	$("#clearAllButton").show();
	$().getHistory(url);
};

$(document).ready(function() {
	$().fillTotal();
	var url=$("#eventUrl").html();
	$("#clearAllButton").click(function(){
		$().clearAll(url);
	});
	$("#voterEmail").focus(function(){
		$().inputEmail();
	});
	$("#voterNote").focus(function(){
		$().inputNote();
	});

	$("#voterName").focus(function(){
		$().inputName();
	});
    
	$("#voterEmail").blur(function(){
		$().getHistory(url);
	});
	
	$("#voterName").blur(function(){
		$().setVoterName();
	});
	$("#modifyEmailButton").click(function(){
		$().modify();
	});
	$("#showHistoryButton").click(function(){
		$().showHistory(url);
	});
	$("#saveButton").click(function(){
		$().updateVote(url);
	});
});
